package com.toweave.starter.activity;


import static com.toweave.starter.utils.ConstantsUtil.LIZ_TAG;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import com.toweave.starter.R;
import com.toweave.starter.fragments.AsBottomSheetDialogFragment;
import com.toweave.starter.fragments.HalfScreenDialogFragment;

public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {

  private TextView tv_alert_dialog;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alert_dialog);

    Button btn_alert_dialog = findViewById(R.id.btn_alert_dialog);
    btn_alert_dialog.setOnClickListener(this);


    Button btn_alert_dialog_user = findViewById(R.id.btn_alert_dialog_user);
    btn_alert_dialog_user.setOnClickListener(this);

    Button btn_alert_dialog_half = findViewById(R.id.btn_alert_dialog_half);
    btn_alert_dialog_half.setOnClickListener(this);

    Button btn_alert_dialog_bottom_sheet = findViewById(R.id.btn_alert_dialog_bottom_sheet);
    btn_alert_dialog_bottom_sheet.setOnClickListener(this);

    tv_alert_dialog = findViewById(R.id.tv_alert_dialog);
    tv_alert_dialog.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.btn_alert_dialog) {
      // 创建对话框构建器
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("提示");
      builder.setMessage("Liz 真的需要测试吗？");
      builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          tv_alert_dialog.setText("确定 测试");
        }
      });
      builder.setNegativeButton("取消", (dialog, which) -> {
        tv_alert_dialog.setText("取消 测试");
      });

      // 创建对话框
      AlertDialog alertDialog = builder.create();
      alertDialog.show();
    } else if (id == R.id.btn_alert_dialog_user) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      // Get the layout inflater.
      LayoutInflater inflater = LayoutInflater.from(this);
      Log.i(LIZ_TAG, "onClick 1: " + id);
      // Inflate and set the layout for the dialog.
      // Pass null as the parent view because it's going in the dialog layout.
      builder.setView(inflater.inflate(R.layout.dialog_signin, null, true))
          // Add action buttons
          .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
              // Sign in the user.
            }
          })
          .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              Log.i(LIZ_TAG, "onClick: " + id);
            }
          });
      // 创建对话框
      AlertDialog alertDialog = builder.create();
      alertDialog.setView(inflater.inflate(R.layout.dialog_signin, null), 0, 100, 1, 1);
      alertDialog.show();
    } else if (id == R.id.btn_alert_dialog_half) {
      // 在 Activity 中显示 DialogFragment
      HalfScreenDialogFragment dialogFragment = new HalfScreenDialogFragment();
      // dialogFragment.show(getSupportFragmentManager(), "halfScreenDialog");
      dialogFragment.show(getSupportFragmentManager(), dialogFragment.getTag());
    } else if (id == R.id.btn_alert_dialog_bottom_sheet) {
      // 显示BottomSheetDialogFragment
      AsBottomSheetDialogFragment bottomSheetDialogFragment = new AsBottomSheetDialogFragment();
      bottomSheetDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.AsBottomSheetDialog);
      bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
    }
  }
}